package com.whfc.mach.redis.impl;

import com.whfc.common.util.JSONUtil;
import com.whfc.mach.dto.AppMachOilSheetDTO;
import com.whfc.mach.redis.MachOilSheetRedisDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.text.MessageFormat;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2021-05-22 14:35
 */
@Repository
public class MachOilSheetRedisDaoImpl implements MachOilSheetRedisDao {

    private static final String machDataLogKey = "mach-oil-sheet::{0}";

    private static final int EXPIRE_HOURS = 1;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void set(Integer machId, List<AppMachOilSheetDTO> oilSheetDTOS) {
        if (oilSheetDTOS != null) {
            String key = MessageFormat.format(machDataLogKey, machId);
            String val = JSONUtil.toString(oilSheetDTOS);
            redisTemplate.opsForValue().set(key, val, EXPIRE_HOURS, TimeUnit.HOURS);
        }
    }

    @Override
    public List<AppMachOilSheetDTO> get(Integer machId) {
        String key = MessageFormat.format(machDataLogKey, machId);
        String val = redisTemplate.opsForValue().get(key);
        return JSONUtil.parseArray(val, AppMachOilSheetDTO.class);
    }

    @Override
    public void delete(Integer machId) {
        String key = MessageFormat.format(machDataLogKey, machId);
        redisTemplate.delete(key);
    }
}
